package com.springboot.controller;

import com.springboot.dto.AssistantSetInfoView;
import com.springboot.dto.DoctorSetInfoView;
import com.springboot.model.DoctorFieldForm;
import com.springboot.model.DoctorInfoForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorSetInfoViewService;
import com.springboot.service.FileUploadService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 本控制台用于响应  用户个人信息设置界面（3种用户：患者，医生，助理）
 */
@Controller
public class UserSetInfoController {
    //声明业务类
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    DoctorSetInfoViewService doctorSetInfoViewService;
    @Autowired
    FileUploadService fileUploadService;

    //患者端个人信息设置界面响应,通过@RequestParam获取参数,显示初始数据
    @CrossOrigin
    @RequestMapping(value = "/patientsettingshow")
    public @ResponseBody
    String patientsettingshow(@RequestParam("unitphone") String tel){
        System.out.println(tel+"正在显示信息");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            System.out.println("当前用户为："+user.toString());
            return user.toString11();
        }
        catch (Exception e)
        {
            return "error!";
        }
    }

    //医生端个人信息设置界面响应,通过@RequestParam获取参数,显示初始数据
    @CrossOrigin
    @RequestMapping(value = "/getDoctorInfoServlet")
    public @ResponseBody
    String doctorsettingshow(@RequestParam("tel") String tel){
        System.out.println(tel+"正在显示信息");
        try{
            DoctorInfoForm doctor=doctorSetInfoViewService.findDoctorsByTel(tel);
            UserLoginForm user=userLoginService.findUserByTel(tel);
            System.out.println(doctor.toString()+" "+doctor.getDoctorId()+"正在显示信息");
            Integer id=doctor.getDoctorId();
            System.out.println(doctor.toString()+" "+doctor.getDoctorId()+"正在显示信息");
            DoctorFieldForm doctorFieldForm=doctorSetInfoViewService.findDoctorsByID(id);
            //DoctorInfoForm doctorInfoForm=doctorSetInfoViewService.findDoctorsByTel(tel);
            String img=user.getLogoLink();
            String name=user.getUserName();
            String department=doctorFieldForm.getDoctorField();
            String hosname="峻宇诊所";//若要改，就要加联系表
            String position=doctor.getDoctorRank();

            System.out.println(doctor.getDoctorRank()+" || "+doctor.getDoctorTel());


            DoctorSetInfoView doctorSetInfoView = new DoctorSetInfoView();//初始化
            doctorSetInfoView.setDoctorLogoLink(img);
            doctorSetInfoView.setDoctorName(name);
            doctorSetInfoView.setDoctorId(id);
            doctorSetInfoView.setDoctorDepartment(department);//科室
            doctorSetInfoView.setDoctorPosition(position);//职称
            doctorSetInfoView.setDoctorhosName(hosname);
            System.out.println(doctorSetInfoView.toString());

            return doctorSetInfoView.toString();
        }
        catch (Exception e)
        {
            return "False";
        }
    }

    //助理个人信息设置界面响应,通过@RequestParam获取参数,显示初始数据
    @CrossOrigin
    @RequestMapping(value = "/getAssistantServlet")
    public @ResponseBody
    String assistantsettingshow(@RequestParam("tel") String tel
    ){
        System.out.println(tel+" "+"助理正在显示信息");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            System.out.println(tel+" "+"助理显示信息");
            String a_img=user.getLogoLink();
            String a_name=user.getUserName();
            Integer a_id=user.getUserId();
            String a_hosname="峻宇诊所";
            System.out.println("当前用户为："+user.toString11());
            AssistantSetInfoView assistantSetInfoView=new AssistantSetInfoView();
            assistantSetInfoView.setDoctorLogoLink(a_img,a_id,a_name,a_hosname);
            System.out.println("用户为："+assistantSetInfoView.toString1());

            return assistantSetInfoView.toString1();
        }
        catch (Exception e)
        {
            return "False";
        }
    }



    //患者端个人信息设置界面响应,通过@RequestParam获取参数，修改保存数据
    @CrossOrigin
    @RequestMapping(value = "/patientsettingTxt")
    public @ResponseBody
    String patientsetinfo(@RequestParam("name") String name,
                          @RequestParam("Indexs") String sex,
                          @RequestParam("unitphone") String tel){
        System.out.println(name+" "+sex+" "+tel +"正在请求登录");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            System.out.println("成功查找到用户"+user.toString());
            user.setUserName(name);
            user.setUserSex(sex);
            //更新登陆表的内容
            System.out.println("成功查找到用户"+user.toString());
            userLoginService.updateUserLoginForm(user);
            System.out.println("成功查找到用户"+user.toString());
            return "true";
        }
        catch (Exception e)
        {
            return "false";
        }
    }

    //患者端个人信息设置界面响应,通过@RequestParam获取参数，修改保存数据
    @CrossOrigin
    @RequestMapping(value = "/patientsettingImg")
    public @ResponseBody
    String patientsetinfo(@RequestParam("unitphone") String tel,
                          @RequestParam("avatar10") MultipartFile file){
        System.out.println(tel +"正在请求登录");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            System.out.println("成功查找到用户"+user.toString());
            String tmpSex;
            String logoLink =fileUploadService.UploadPicture(file,"personInfotel="+tel);
            user.setLogoLink(logoLink);
            //更新登陆表的内容
            System.out.println("成功查找到用户"+user.toString());
            userLoginService.updateUserLoginForm(user);
            System.out.println("成功查找到用户"+user.toString());
            return "true";
        }
        catch (Exception e)
        {
            return "false";
        }
    }

    //医生端个人信息设置界面响应,通过@RequestParam获取参数，修改保存数据
    @CrossOrigin
    @RequestMapping(value = "/setupServlet")
    public @ResponseBody
    String doctorlogin1(@RequestParam("department") String department,
                       @RequestParam("tel") String tel,
                        @RequestParam("name") String name,
                       @RequestParam("position") String position){
        System.out.println(tel+" "+"医生正在修改信息");
        try{
            DoctorInfoForm doctor=doctorSetInfoViewService.findDoctorsByTel(tel);
            int doctor_id=doctor.getDoctorId();
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            userLoginForm.setUserName(name);
            DoctorFieldForm doctorFieldForm=doctorSetInfoViewService.findDoctorsByID(doctor_id);
            DoctorSetInfoView doctorSetInfoView = new DoctorSetInfoView();//初始化
            doctorSetInfoView.setDoctorDepartment(department);//科室
            doctorSetInfoView.setDoctorPosition(position);//职称
            doctor.setDoctorRank(position);
            doctor.setDoctorName(name);
            doctorFieldForm.setDoctorField(department);

            doctorSetInfoViewService.insertDoctorField(doctorFieldForm);
            doctorSetInfoViewService.insertDoctorRank(doctor);
            userLoginService.updateUserLoginForm(userLoginForm);


            return "true";

        }
        catch (Exception e)
        {
            return "False";
        }
    }

    //医生端个人信息设置界面响应,通过@RequestParam获取参数，修改保存数据
    @CrossOrigin
    @RequestMapping(value = "/setImgServlet")
    public @ResponseBody
    String doctorlogin2(@RequestParam("avatar100") MultipartFile file,
                       @RequestParam("tel") String tel){
        System.out.println(tel+" "+"医生正在修改图片信息");
        try{
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            String img =fileUploadService.UploadPicture(file,"personInfotel="+tel);
            userLoginForm.setLogoLink(img);
            userLoginService.updateUserLoginForm(userLoginForm);
            return "true";

        }
        catch (Exception e)
        {
            return "False";
        }
    }

   //助理个人信息设置界面响应,通过@RequestParam获取参数,修改保存数据
    @CrossOrigin
    @RequestMapping(value = "/assistantSetNameServlet")
    public @ResponseBody
    String assistantsetting(@RequestParam("Tel") String tel,
                            @RequestParam("name") String name){
        System.out.println(tel+" "+name+"助理正在修改信息");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            String username=user.getUserName();
            String imgs=user.getLogoLink();
            System.out.println("当前用户为："+username+" "+imgs);
            AssistantSetInfoView assistantSetInfoView=new AssistantSetInfoView();
            assistantSetInfoView.setName(name);
            assistantSetInfoView.setHosName("峻宇诊所");
            assistantSetInfoView.setId(user.getUserId());
            user.setUserName(name);
            userLoginService.updateUserLoginForm(user);
            return "True";
        }
        catch (Exception e)
        {
            return "False";
        }
    }

    //助理个人信息设置界面响应,通过@RequestParam获取参数,修改保存头像数据
    @CrossOrigin
    @RequestMapping(value = "/updateAssistantImgServlet")
    public @ResponseBody
    String assistantsetting(@RequestParam("Tel") String tel,
                            @RequestParam("avatar50") MultipartFile file){
        System.out.println(tel+" "+"助理正在修改信息");
        try{
            UserLoginForm user = userLoginService.findUserByTel(tel);
            String username=user.getUserName();
            String imgs=user.getLogoLink();
            System.out.println("当前用户为："+username+" "+imgs);
            AssistantSetInfoView assistantSetInfoView=new AssistantSetInfoView();
            String a =fileUploadService.UploadPicture(file,"personInfotel="+tel);//照片存储路径
            assistantSetInfoView.setImg(a);
            assistantSetInfoView.setHosName("峻宇诊所");
            assistantSetInfoView.setId(user.getUserId());
            user.setLogoLink(a);
            userLoginService.updateUserLoginForm(user);
            return "True";
        }
        catch (Exception e)
        {
            return "False";
        }
    }
}
